/**
 * 
 */
package com.sqm.timecard;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Hashtable;
import java.util.SortedMap;
import java.util.TreeMap;



/**
 * @author qiming
 * 
 * This object describe one person's monthly report.
 *
 */
public class PersonalMonthlyReport
{
    public static final String FORGET="FORGET";
    public static final String LATE="LATE";
    public static final String EARLY="EARLY";
    public static final String OVERTIME="OVERTIME";
    public static final String VACATION="VACATION";
      
    private String employeeId;
    private String employeeName;
    private Hashtable<String,Integer> statItems =new Hashtable<String,Integer>();
    private Hashtable<String,Collection<Calendar>> statDetaildDays =new Hashtable<String,Collection<Calendar>>();
    
    private SortedMap<Integer, DailyRecord> detailedRecords = new TreeMap<Integer,DailyRecord>();
    
    private int validWorkDayNumber=0;
    private int totalWorkTime=0;
    
    public PersonalMonthlyReport(String employeeId, String employeeName)
    {
        super();
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    }
    public PersonalMonthlyReport(String employeeId)
    {
        super();
        this.employeeId = employeeId;
    }
    public DailyRecord getDailyRecordByDate(int dayOfYear)
    {
        return detailedRecords.get(dayOfYear);
    }
    public void addDailyRecord(int dayOfYear, DailyRecord record)
    {
        detailedRecords.put(dayOfYear, record);
    }
    
    public String getEmployeeId()
    {
        return employeeId;
    }
    public void setEmployeeId(String employeeId)
    {
        this.employeeId = employeeId;
    }
    public String getEmployeeName()
    {
        return employeeName;
    }
    public void setEmployeeName(String employeeName)
    {
        this.employeeName = employeeName;
    }
    public void increaseTimes( String type,int times)
    {
        int temp = 0;
        if(statItems.get(type)==null)
        {
            temp=0;
        }
        else
            temp=statItems.get(type);
        temp+=times;
        statItems.put(type, temp);
    }
    
    /**
     * Record each days by type if it's not a normal days
     * @param type: late,early,forget or overtime
     * @param day
     */
    public void recordStatDetailedDays( String type,Calendar day)
    {
        Collection<Calendar> days = statDetaildDays.get(type);
        if( days == null )
        {
            days = new ArrayList<Calendar>();
            statDetaildDays.put(type,days);
        }
        days.add(day);
    }
    
    public Collection<Calendar> getStatDetailedDays( String type )
    {
        return statDetaildDays.get(type);
    }
    
    public int getStatItems(String type)
    {
        return statItems.get(type)==null?0:statItems.get(type);
    }
    public SortedMap<Integer, DailyRecord> getDetailedRecords()
    {
        return detailedRecords;
    }
    
    private String getStatDetailDaysString(String type)
    {
        Collection<Calendar> days = statDetaildDays.get(type);
        String ret = "";
        if (days != null && days.size() > 0)
        {
            SimpleDateFormat sdf = new SimpleDateFormat(
                    TimecardProcesser.DISPLAY_DATE_FORMAT);
            for (Calendar day : days)
            {
                ret += sdf.format(day.getTime()) + ",";
            }
        }
        return ret;
    }
    
    public String getDonative()
    {
        return ((this.getStatItems(LATE)+this.getStatItems(EARLY)+this.getStatItems(FORGET)-3)<0?0:(this.getStatItems(LATE)+this.getStatItems(EARLY)+this.getStatItems(FORGET)-3)*10)+"";
    }
    
    public String getSummary()
    {
        StringBuffer str=new StringBuffer();
        String line=System.getProperty("line.separator");
        
        if( this.getStatItems(LATE) == 0 &&
                this.getStatItems(EARLY)==0&&
                this.getStatItems(FORGET)==0 )
            return "Normal";
            
        str.append("[LATE]:");
        str.append(this.getStatItems(LATE));
        str.append("  [Days]:");
        str.append(getStatDetailDaysString(LATE));
        str.append(line);

        str.append("[EARLY]:");
        str.append(this.getStatItems(EARLY));
        str.append("  [Days]:");
        str.append(getStatDetailDaysString(EARLY));
        str.append(line);
        
        str.append("[FORGET]:");
        str.append(this.getStatItems(FORGET));
        str.append(" [Days]:");
        str.append(getStatDetailDaysString(FORGET));
        str.append(line);
        
//        
//        str.append(this.getStatItems(LATE));
//        str.append(line);
        
        return str.toString(); 
    }
	/**
	 * @return the avgWorkTimePerDay, by hour
	 */
	public String getAvgWorkTimePerDay()
	{
		float tmpRet = 0.00f;
		if (validWorkDayNumber != 0)
		{
			tmpRet = (float) totalWorkTime / (float) (validWorkDayNumber * 60);
		}
		java.math.BigDecimal bb = new java.math.BigDecimal(tmpRet);
		String ret = bb.setScale(2, java.math.BigDecimal.ROUND_HALF_EVEN)
				.toString();

		return ret;
	}

	/**
	 * @return the validWorkDayNumber
	 */
	public int getValidWorkDayNumber()
	{
		return validWorkDayNumber;
	}
	/**
	 * @param validWorkDayNumber the validWorkDayNumber to set
	 */
	public void setValidWorkDayNumber(int validWorkDayNumber)
	{
		this.validWorkDayNumber = validWorkDayNumber;
	}
	/**
	 * @return the totalWorkTime
	 */
	public int getTotalWorkTime()
	{
		return totalWorkTime;
	}
	/**
	 * @param totalWorkTime the totalWorkTime to set
	 */
	public void setTotalWorkTime(int totalWorkTime)
	{
		this.totalWorkTime = totalWorkTime;
	}

	/**
	 * @return the totalWorkTime
	 */
	public int getTotalWorkTimeByHour()
	{
		return totalWorkTime/60;
	}
}
